import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/config/config.dart';
import 'package:xvideos/hj_page/find/acg_game_detail_page.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/global_store/store.dart';

import '../../../hj_model/splash/ads_model.dart';
import '../../../hj_model/video_model.dart';
import '../../../tools_base/widget/net_image_widget.dart';

class VideoSimpleCellWidget extends StatelessWidget {
  final VideoModel? videoModel;
  final BorderRadius? imgBorderRadius;
  final double? imgAspectRatio; // 图片按照AspectRatio 来处理
  final bool isEditStatus; // 编辑模式
  final GestureTapCallback? onTap;
  final int textLines;
  final bool isFromHY; // 黄油风格
  final bool isShowBottom; // true 显示底部标签和评论数
  final bool isFromSearch;

  const VideoSimpleCellWidget({
    super.key,
    this.videoModel,
    this.imgBorderRadius,
    this.imgAspectRatio,
    this.isEditStatus = false,
    this.textLines = 2,
    this.onTap,
    this.isFromHY = false,
    this.isShowBottom = true,
    this.isFromSearch = false,
  });

  bool get isDarkStyle {
    if (isFromSearch) {
      return videoModel?.isDarkTag ?? false;
    } else {
      return false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap ??
          () {
            if (videoModel?.isRandomAd() == true) {
              pushToPageByLink(videoModel?.randomAdsInfo?.href, id: videoModel?.randomAdsInfo?.id);
              return;
            } else if (isDarkStyle && !globalStore.isAWVIP()) {
              jumpToWalletPage(vipId: Config.darkWebVipId);
            } else {
              if (videoModel?.newsType == 'SEED_LINK') {
                Get.to(() => AcgGameDetailPage(id: videoModel?.id ?? ''), preventDuplicates: false);
              } else {
                pushToVideoPage(videoModel: videoModel);
              }
            }
          },
      child: videoModel?.isRandomAd() == true
          ? VideoSimpleAdsCellWidget(
              videoModel: videoModel?.randomAdsInfo,
              textLines: textLines,
            )
          : _buildContent(),
    );
  }

  Widget _buildContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        if (imgAspectRatio != null)
          AspectRatio(
            aspectRatio: imgAspectRatio!,
            child: _buildCoverItem(),
          )
        else
          Expanded(
            child: _buildCoverItem(),
          ),
        Container(
          padding: const EdgeInsets.only(top: 4),
          child: imgAspectRatio != null
              ? Text(
                  videoModel?.title ?? "",
                  maxLines: textLines,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 12,
                  ),
                )
              : Text(
                  "${videoModel?.title ?? ""}\n",
                  maxLines: textLines,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Colors.white.withOpacity(0.9),
                    fontSize: 12,
                  ),
                ),
        ),
        if (isShowBottom) _buildBottomWidget(),
        _TestEmpty(),
      ],
    );
  }

  Widget _buildCoverItem() {
    return Stack(
      fit: StackFit.expand,
      children: [
        NetworkImageLoader(
          imageUrl: videoModel?.cover ?? "",
          imgBorderRadius: imgBorderRadius,
          borderRadius: 4,
          blur: isDarkStyle && !globalStore.isAWVIP(),
        ),
        Positioned(
          left: 0,
          right: 0,
          bottom: 0,
          child: Container(
            height: 24,
            padding: EdgeInsets.symmetric(horizontal: 6),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.vertical(bottom: Radius.circular(4)),
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Colors.black.withOpacity(0),
                  Colors.black.withOpacity(0.6),
                ],
              ),
            ),
            child: isFromHY ? _buildCoverHYBottom() : _buildCoverNormalBottom(),
          ),
        ),
        if (isEditStatus)
          Container(
            color: Colors.black.withOpacity(0.5),
            padding: EdgeInsets.only(top: 6, right: 6),
            alignment: Alignment.topRight,
            child: Image.asset(
              videoModel?.isSelected == true ? "selected_green.webp".commonImgPath : "empty_circle.webp".commonImgPath,
              width: 24,
              height: 24,
            ),
          ),
        if (isDarkStyle && !globalStore.isAWVIP())
          Container(
            color: Colors.black.withOpacity(0.1),
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 8),
            child: RichText(
              textAlign: TextAlign.center,
              text: TextSpan(
                text: '需要开通 ',
                children: [
                  TextSpan(
                    text: '${Config.darkWebVipName}\n',
                    style: TextStyle(color: Color(0xffE1351F)),
                  ),
                  TextSpan(text: '即可观看'),
                ],
                style: TextStyle(
                  fontSize: 10,
                  color: Colors.white,
                  height: 1.6,
                ),
              ),
            ),
          ),
      ],
    );
  }

  Widget _buildCoverNormalBottom() {
    return Row(
      children: [
        Image.asset("eye_icon.webp".homePath, width: 16),
        1.sizeBoxW,
        Text(
          "${videoModel?.playCount?.humanizeFormat ?? "0"}",
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(color: Colors.white, fontSize: 10, height: 1.6),
        ),
        Spacer(),
        Text(
          videoModel?.playTime?.timeMSFormat ?? "",
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            color: Colors.white,
            fontSize: 10,
          ),
        ),
      ],
    );
  }

  Widget _buildCoverHYBottom() {
    return Row(
      children: [
        Image.asset("eye_icon.webp".homePath, width: 16),
        1.sizeBoxW,
        Text(
          "${videoModel?.pageViewCount?.humanizeFormat ?? "0"}",
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(color: Colors.white, fontSize: 10, height: 1.6),
        ),
        Spacer(),
      ],
    );
  }

  Widget _buildBottomWidget() {
    if (isFromHY) {
      String tagDesc = videoModel?.tags?.isNotEmpty == true ? (videoModel!.tags!.first.name ?? "") : "";
      for (int i = 1; i < (videoModel?.tags?.length ?? 0); i++) {
        tagDesc += ("·" + (videoModel!.tags![i].name ?? ""));
      }
      return Container(
        padding: EdgeInsets.only(top: 3),
        child: Text(
          tagDesc,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            color: Color(0x73FFFFFF),
            fontSize: 10,
          ),
        ),
      );
    } else {
      return Container(
        padding: EdgeInsets.only(top: 3),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (videoModel?.tags?.isNotEmpty == true)
              Container(
                padding: EdgeInsets.symmetric(horizontal: 4, vertical: 1),
                decoration: BoxDecoration(
                  color: Color(0x0DFFFFFF),
                  borderRadius: BorderRadius.circular(3),
                ),
                child: Text(
                  videoModel?.tags?.first.name ?? "",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Color(0x73FFFFFF),
                    fontSize: 10,
                  ),
                ),
              ),
            Spacer(),
            Text(
              "评论${videoModel?.commentCount?.humanizeFormat}",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: Color(0x73FFFFFF),
                fontSize: 10,
              ),
            ),
          ],
        ),
      );
    }
  }

  Widget _buildPriceItem() {
    if (videoModel?.permission == 2) {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(8),
            bottomLeft: Radius.circular(8),
          ),
          gradient: LinearGradient(
            colors: [
              Color(0xffADFCAC),
              Color(0xff5CEF97),
            ],
          ),
        ),
        child: Text(
          "免费",
          style: TextStyle(
            color: Colors.black,
            fontSize: 10,
          ),
        ),
      );
    } else if (videoModel?.permission == 1) {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 6, vertical: 3),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(8),
            bottomLeft: Radius.circular(8),
          ),
          gradient: LinearGradient(
            colors: [
              Color(0xffDB361F),
              Color(0xffDB361F),
            ],
          ),
        ),
        child: Image.asset("coin.webp".commonImgPath, width: 14, height: 14),
      );
    } else {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(8),
            bottomLeft: Radius.circular(8),
          ),
          gradient: LinearGradient(
            colors: [
              Color(0xffF6CBB8),
              Color(0xffEEB4AA),
            ],
          ),
        ),
        child: Text(
          "VIP",
          style: TextStyle(
            color: Colors.black,
            fontSize: 12,
          ),
        ),
      );
    }
  }
}

class VideoSimpleAdsCellWidget extends StatelessWidget {
  final AdsInfoModel? videoModel;
  final bool isShowTime;
  final int textLines;

  const VideoSimpleAdsCellWidget({super.key, this.videoModel, this.isShowTime = true, this.textLines = 2});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        pushToPageByLink(videoModel?.href, id: videoModel?.id);
      },
      child: LayoutBuilder(
        builder: (ctx, constraints) {
          // isHor true: 大横屏广告
          bool isHor = (screen.screenWidth - 32) <= constraints.maxWidth;
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Stack(
                  fit: StackFit.expand,
                  children: [
                    NetworkImageLoader(
                      imageUrl: videoModel?.cover ?? "",
                      borderRadius: 4,
                      width: double.infinity,
                      height: double.infinity,
                    ),
                    Positioned(
                      top: 0,
                      left: 0,
                      child: Container(
                        width: 34,
                        height: 16,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            gradient: LinearGradient(colors: [
                              Color.fromRGBO(255, 235, 58, 1),
                              Color.fromRGBO(255, 235, 58, 1),
                            ]),
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(4),
                            )),
                        child: Text(
                          "广告",
                          style: TextStyle(
                            fontSize: 11,
                            color: Color.fromRGBO(0, 0, 0, 1),
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                padding: const EdgeInsets.only(top: 4),
                child: Text(
                  isHor ? "${videoModel?.title ?? ""}" : "${videoModel?.title ?? ""}\n",
                  maxLines: textLines,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 12,
                  ),
                ),
              ),
              if (!isHor)
                Container(
                  padding: const EdgeInsets.only(top: 3),
                  child: Text(
                    "",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 10,
                    ),
                  ),
                ),
            ],
          );
        },
      ),
    );
  }
}

class _TestEmpty extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TestEmptyState();
  }
}

class _TestEmptyState extends State<_TestEmpty> {
  @override
  Widget build(BuildContext context) {
    return SizedBox();
  }

  @override
  void dispose() {
    super.dispose();
  }
}
